﻿using PerformanceExamine.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PerformanceExamine
{
    public delegate void DeleBindDgv();//声明委托
    public partial class FormUserManager : Form
    {
        DeleBindDgv deleBindDgv;
        public FormUserManager()
        {
            InitializeComponent();
        }

        private void FormUserManager_Load(object sender, EventArgs e)
        {
            //加载身份的下拉列表
            bindComboBoxIdentity();
            //加载datagridView数据
            bindDataGridView();
            deleBindDgv = bindDataGridView;
        }

        private void bindDataGridView()
        {
            //筛选
            string userName = txtUser.Text.Trim();
            int baseTypeId = (int)comboBoxIdentity.SelectedValue;
            bool isStop = checkBoxSuspension.Checked;

            //加载datagridView数据
            dgvUserAppraisal.AutoGenerateColumns = false;
            List<UserAppraisalBase> userAppraisalBases = UserAppraisalBase.GetListJoinAppraisal();
            if (baseTypeId == 0 && checkBoxSuspension.Checked == false)
            {
                dgvUserAppraisal.DataSource = userAppraisalBases.FindAll(m=>m.IsDel == false);
            }
            else if (baseTypeId == 0 && checkBoxSuspension.Checked == true)
            {
                dgvUserAppraisal.DataSource = userAppraisalBases.FindAll(m => m.IsDel == true);
            }
            else if (checkBoxSuspension.Checked == false)
            {
                dgvUserAppraisal.DataSource = userAppraisalBases.FindAll(m => m.UserName.Contains(userName) && m.IsDel == isStop && m.BaseTypeId == baseTypeId&&m.IsDel == false);
            }
            else
            {
                dgvUserAppraisal.DataSource = userAppraisalBases.FindAll(m => m.UserName.Contains(userName) && m.IsDel == isStop && m.BaseTypeId == baseTypeId && m.IsDel == true);
            }
        }

        private void bindComboBoxIdentity()
        {
            List<AppraisalBases> appraisalBases = new List<AppraisalBases>();
            appraisalBases = AppraisalBases.ListAll();
            appraisalBases.Insert(0, new AppraisalBases()
            {
                Id = 0,
                BaseType = "-查询所有-",
                AppraisalBase = 0,
                IsDel = false
            });
            comboBoxIdentity.DataSource = appraisalBases;
            comboBoxIdentity.DisplayMember = "BaseType";
            comboBoxIdentity.ValueMember = "Id";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            bindDataGridView();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void dgvUserAppraisal_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button==MouseButtons.Right)
            {
                tsmAdd.Visible = true;
                tsmEdit.Visible = false;
                tsmStart.Visible = false;
                tsmStop.Visible = false;
            }
        }

        private void dgvUserAppraisal_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tsmAdd.Visible = true;
                tsmEdit.Visible = true;
                bool isDel = (bool)dgvUserAppraisal.SelectedRows[0].Cells["IsDel"].Value;
                if (isDel)
                {
                    tsmStart.Visible = true;
                }
                else
                {
                    tsmStop.Visible = false;
                }     
            }
        }

        private void tsmAdd_Click(object sender, EventArgs e)
        {
            FormSetUser formSetUser = new FormSetUser(deleBindDgv);
            formSetUser.ShowDialog();
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            int id = (int)dgvUserAppraisal.SelectedRows[0].Cells["Id"].Value;
            FormSetUser formSetUser = new FormSetUser(deleBindDgv,id);
            formSetUser.ShowDialog();
        }
    }
}
